﻿Imports System.Net
Imports System.IO
Imports System.Threading
Imports System.Diagnostics
Public Class DAUSUltimate

#Region " [ 맴버 변수들 ] "
    Public FileName As String = Nothing '현재 프로그램 경로
    Public Version As Double = Nothing '프로그램 버전
    Public SetupFile As String = Nothing 'SetupFile위치
    Private WithEvents MWC As WebClient = New WebClient
    Public Theme As Themes
    Public IsAdminestor As Boolean = False
    Public Event DownLoadComplete()
    Public Event DownLoadProgressChange(ByVal DownLoadProgress As Integer)
#End Region

#Region " [ 주요 메서드 들 ] "
    Public Function UpdateCheck() As Boolean
        Dim str = GetSetUp()
        Dim Setup = str.Split("|")
        Dim NewVer As Double = Convert.ToDouble(Setup(0))
        If NewVer > Version Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetInfo() As VersionInfo 'INFO생성
        Dim V As New VersionInfo
        Dim str = GetSetUp()
        Dim setup = str.Split("|")

        V.NewVersion = setup(0).ToString
        V.ProgramName = setup(1).ToString
        V.Description = setup(2).ToString

        Return V
    End Function

    Public Sub Start()
        Dim Mythread As New Thread(AddressOf Work)
        Mythread.Start()
    End Sub

    Private Sub Work()
        Dim wc As New WebClient
        wc.DownloadFile(SetupFile, "SetUp.txt")
        Dim sw As StreamWriter = New StreamWriter("Path.txt", False, System.Text.Encoding.Default)
        sw.Write(Path.GetFileName(FileName))
        sw.Flush()
        sw.Close()
        Dim uri As Uri = New Uri("https://www.dropbox.com/s/poj8bvdhzcorq5p/DAUS%28Updater%29.exe?dl=1")
        Select Case Theme
            Case Themes.SteveOS
                If IsAdminestor = False Then
                    uri = New Uri("https://www.dropbox.com/s/exhlticvlurlzty/DAUS%28Updater%29.exe?dl=1")
                Else
                    uri = New Uri("https://www.dropbox.com/s/bmsmad629272rg3/DAUS%28Updater%29-adminestor.exe?dl=1")
                    '관리자 권한 프로그램
                End If
            Case Themes.UniqueBlack
                If IsAdminestor = False Then
                    uri = New Uri("https://www.dropbox.com/s/poj8bvdhzcorq5p/DAUS%28Updater%29.exe?dl=1")
                Else
                    uri = New Uri("https://www.dropbox.com/s/yezqpn99b6cdq15/Adminestor.exe?dl=1")
                    '관리자 권한 프로그램
                End If
            Case Themes.WhiteOrange
                If IsAdminestor = False Then
                    uri = New Uri("https://www.dropbox.com/s/l83t4j63qixutov/DAUS%20V2.0%20Theme%28WhiteOrange%29.exe?dl=1")
                Else
                    uri = New Uri("https://www.dropbox.com/s/ujgim7sf698m2f3/Adminestor.exe?dl=1")
                    '관리자 권한 프로그램
                End If
        End Select
        MWC.DownloadFileAsync(uri, "Updater.exe")
    End Sub
#End Region

#Region " [ 파일 다운로드 관련 ] "
    Private Sub Download(ByVal URL As String, ByVal PATH As String)
        Dim wc As WebClient = New WebClient
        Dim uri As Uri = New Uri(URL)
        wc.DownloadFileAsync(uri, PATH)
    End Sub
    Private Sub MWC_DownloadFileCompleted(sender As Object, e As ComponentModel.AsyncCompletedEventArgs) Handles MWC.DownloadFileCompleted
        Dim Prc As New Process()
        Prc.StartInfo.FileName = "Updater.exe"
        Try
            Prc.Start()
        Catch ex As Exception
            MsgBox("관리자 권한을 얻는데 실패하였습니다.", MsgBoxStyle.Critical, "알림")
        End Try
        RaiseEvent DownLoadComplete()
    End Sub

    Private Sub MWC_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles MWC.DownloadProgressChanged
        RaiseEvent DownLoadProgressChange(e.ProgressPercentage)
    End Sub


#End Region

#Region " [ 기타 메서드들 ] "
    Private Function GetSetUp() As String
        Dim wc As WebClient = New WebClient
        Dim uri As Uri = New Uri(SetupFile)
        Dim str = wc.DownloadString(uri)
        Return str
    End Function
    Public Sub Clear()
        Try
            My.Computer.FileSystem.DeleteFile("Updater.exe")
        Catch ex As Exception
        End Try
    End Sub
#End Region

End Class

#Region " [ 기타 클래스, 열거형들 ] "
Public Class VersionInfo
    Public NewVersion As String
    Public ProgramName As String
    Public Description As String
End Class

Public Enum Themes
    SteveOS
    UniqueBlack
    WhiteOrange
End Enum
#End Region




